package controller.business.inspectorAssess;

import controller.business.observerAssess.ObserverAssessController;
import controller.common.BaseController;
import controller.common.StaticConstant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.business.BusinessInfoServices;
import services.business.InspectorAssessServices;
import services.business.InspectorInfoServices;
import wt.entity.business.*;
import wt.staticData.ShortData;

import java.util.*;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("business/inspectorAssess")
public class InspectorAssessController extends BaseController {
    @Autowired
    InspectorAssessServices inspectorAssessServices;
    @Autowired
    InspectorInfoServices inspectorInfoServices;
    @Autowired
    BusinessInfoServices businessInfoServices;


    @RequiresPermissions("business:inspectorAssess:default")
    @RequestMapping("index")
    public String index() throws Exception {
        return "business/inspectorAssess/index";
    }


    @RequiresPermissions("business:inspectorAssess:default")
    @RequestMapping("editDlg")
    public String editDlg(@RequestParam(value = "businessId") String businessId, Map map) throws Exception {

        List<InspectorInfo> inspectorInfoList = inspectorInfoServices.selectByBusinessId(businessId);

        InspectorInfo self = null;
        for (int i = 0; i < inspectorInfoList.size(); i++) {
            InspectorInfo inspectorInfo = inspectorInfoList.get(i);
            if (getLoginAccount().getExpertBase().getId().equals(inspectorInfo.getExpertId())) {
                self = inspectorInfo;
                inspectorInfoList.remove(i);//移除自己
                if ("crew".equals(inspectorInfo.getExpertRole())) {//如果自己是组员移除其他所有组员
                    for (int j = 0; j < inspectorInfoList.size(); j++) {
                        InspectorInfo info = inspectorInfoList.get(j);
                        if ("crew".equals(info.getExpertRole())) {
                            inspectorInfoList.remove(j);
                            j--;
                        }
                    }
                }
                break;
            }
        }


        map.put("data", inspectorInfoList);
        map.put("businessId", businessId);
        if (self!=null){
            map.put("fromInspectorId", self.getId());
        }
        map.put(StaticConstant.EDIT_URL, "business/inspectorAssess/insert");
        map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        return "business/inspectorAssess/editDlg";
    }


    @RequiresPermissions("business:inspectorAssess:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult insert(InspectorAssessList inspectorAssessList, String businessId, String fromInspectorId) throws Exception {

        CommonResult commonResult = CommonResult.newCommonResult();


        BusinessInfo businessInfo = businessInfoServices.selectByPrimaryKey(businessId);
        if (inspectorAssessList.getInspectorAssessList().size() != 1 && inspectorAssessList.getInspectorAssessList().size() != businessInfo.getInspectorCount() - 1) {
            commonResult.addMessage("专家与业务分配专家不匹配");
            return commonResult.setError();
        }


        InspectorAssessExample inspectorAssessExample = new InspectorAssessExample();
        inspectorAssessExample.createCriteria().andBusinessIdEqualTo(businessId).andFromInspectorIdEqualTo(fromInspectorId);
        int count = inspectorAssessServices.countByExample(inspectorAssessExample);
        if (count != 0) {
            commonResult.addMessage("已评价");
            return commonResult.setError();
        }


        for (InspectorAssess inspectorAssess : inspectorAssessList.getInspectorAssessList()) {
            inspectorAssess.setAssessTime(new Date());
            inspectorAssess.setBusinessId(businessId);
            inspectorAssess.setFromInspectorId(fromInspectorId);
            int comprehensive = ObserverAssessController.countComprehensive(inspectorAssess.getCooperation(), inspectorAssess.getBusiness(), inspectorAssess.getFairness(), inspectorAssess.getHonest());
            inspectorAssess.setComprehensive(comprehensive);
        }

        inspectorAssessServices.insertSelective(inspectorAssessList.getInspectorAssessList(), businessId);

        return commonResult.setSuccess();
    }


    @RequiresPermissions("business:inspectorAssess:default")
    @ResponseBody
    @RequestMapping("select")
    public List<BusinessInfo> select() throws Exception {

        InspectorInfoExample inspectorInfoExample = new InspectorInfoExample();
        inspectorInfoExample.createCriteria().andExpertIdEqualTo(getLoginAccount().getExpertBase().getId());
        List<InspectorInfo> inspectorInfoList = inspectorInfoServices.selectByExample(inspectorInfoExample);
        if (inspectorInfoList.isEmpty()) {
            return new ArrayList<BusinessInfo>();
        }

        Set<String> businessIdSet = new HashSet<String>();
        List<String> inspectorIdList = new ArrayList<String>();
        for (InspectorInfo inspectorInfo : inspectorInfoList) {
            businessIdSet.add(inspectorInfo.getBusinessId());
            inspectorIdList.add(inspectorInfo.getId());
        }


        InspectorAssessExample inspectorAssessExample = new InspectorAssessExample();
        inspectorAssessExample.createCriteria().andFromInspectorIdIn(inspectorIdList);
        List<InspectorAssess> inspectorAssessList = inspectorAssessServices.selectByExample(inspectorAssessExample);
        Set<String> businessIdSet1 = new HashSet<String>();
        for (InspectorAssess inspectorAssess : inspectorAssessList) {
            businessIdSet1.add(inspectorAssess.getBusinessId());
        }


        List<String> businessIdList = new ArrayList<String>();
        for (String businessId : businessIdSet) {
            if (!businessIdSet1.contains(businessId)) {
                businessIdList.add(businessId);
            }
        }

        if (businessIdList.size() > 0) {
            BusinessInfoExample businessInfoExample = new BusinessInfoExample();
            businessInfoExample.createCriteria().andIdIn(businessIdList).andBusinessWorkFlowEqualTo(ShortData.s3);
            return businessInfoServices.selectByExample(businessInfoExample);
        } else {
            return new ArrayList<BusinessInfo>();
        }


    }

    @RequiresPermissions("business:inspectorAssess:default")
    @ResponseBody
    @RequestMapping("welcomeTips")
    public CommonResult welcomeTips() throws Exception {
        CommonResult commonResult = new CommonResult();
        int count = select().size();
        if (count == 0) {
            return commonResult.setError();
        } else {
            Map map = new HashMap();
            map.put("info", count + "份评价未填写");
            map.put("url", "business/inspectorAssess/index");
            commonResult.setData(map);
            return commonResult.setSuccess();
        }
    }


}
